package org.projects.graduates.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.projects.graduates.page.Pager;

import com.dayatang.domain.AbstractEntity;
@Entity
@Table(name = "postes")
public class Post extends AbstractEntity implements Comparable<Post> {

	private static final long serialVersionUID = 9149271984188345420L;
	private final static String QL = "select count(o.id) from org.projects.graduates.domain.Post o ";
	@OneToOne
	private Person who;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="up_date")
	private Date upDate;

	@Lob
	private String content;
	
	public static Pager pagerOfAll(long currentPage, long pageSize) {
		return new Pager(count(), pageSize, currentPage);
	}

	public static long count(){
		return count(null);
	}
	
	@SuppressWarnings("unchecked")
	public static long count(String query) {
		String ql = QL + query;
		Map<String, Object> params = new HashMap<String, Object>();
		@SuppressWarnings("rawtypes")
		List<Long> count = new ArrayList(getRepository().find(ql, params));
		return null == count ? 0 : count.get(0).intValue();
	}
	
	public int compareTo(Post other) {
		return new CompareToBuilder().append(getId(), other.getId()).toComparison();
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(this.getId()).toHashCode();
	}

	@Override
	public boolean equals(Object other) {
		if(null == other){
			return false;
		}
		if(other instanceof Post){
			Post o = (Post)other;
			if(o.getId() ==this.getId()){
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return  "content : " + this.content + " update" + this.upDate;
	}

	public Person getWho() {
		return who;
	}

	public void setWho(Person who) {
		this.who = who;
	}

	public Date getUpDate() {
		return upDate;
	}

	public void setUpDate(Date upDate) {
		this.upDate = upDate;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}


}
